"""
    获取公司详情的url，保存进redis队列钟
"""
import redis, pymongo, time
from bson import ObjectId


def write_log(content):
    with open('queue_log.log', 'a', encoding='utf-8') as fp:
        fp.write(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} --> {content}\n')


def main():
    # redis_server = redis.Redis(host='192.168.2.121', port=6379)
    redis_server = redis.Redis(host='127.0.0.1', port=6379)
    mongo_db = pymongo.MongoClient(host='47.94.86.71', port=27017)


    start_id = '606fe88d707c528d6cd26957'


    write_log(f'开始添加队列： -{start_id}')
    write_log('添加100000条队列到redis中！！')
    for queue in mongo_db['tmp_tyc_queue']['tyc_queue'].find().sort('_id', 1):
        name, _id = queue['queue_member'].split('-*-')[1], queue['queue_member'].split('-*-')[0]
        if redis_server.sadd('www', queue["_id"]):
            # tyc_detail_url = f'https://www.tianyancha.com/company/{queue["id"]}-*-{name}'
            redis_server.lpush('tyc_detail_url', queue['queue_member'])
            print(_id, queue['queue_member'])
        else:
           continue
    write_log(f'队列添加完成！ 断点：{name} - {_id}')


if __name__ == '__main__':
    main()
    # redis_server = redis.Redis(host='192.168.2.121', port=6379)
    # mongo_db = pymongo.MongoClient(host='192.168.2.212', port=27500)
    # mongo_db['admin'].authenticate('admin', 'kake')
    # db = mongo_db['tyc_detail_html']['2021-05-21'].find()
    # for i in db:
    #     redis_server.sadd('www', i['id'])
    #     print(i['id'])